<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */

function mobile_service_village_activity_list($parameters){
    global $user;
    $query = db_select('addr', 'A');
    $query->join('village_activity', 'V', 'A.addr_id=V.addr_id');
    $query->join('distributor', 'D', 'D.distributor_id=V.distributor_id');
    //地址级别查询
    if (!empty($parameters['village'])) {//村落级
        $query->condition('A.village_id', $parameters['village'], '=');
    } else if (!empty($parameters['town'])) {//乡镇级
        $query->condition('A.town_id', $parameters['town'], '=');
    } elseif (!empty($parameters['county'])) {//县级
        $query->condition('A.county_id', $parameters['county'], '=');
    } elseif (!empty($parameters['city'])) {//市级
        $query->condition('A.city_id', $parameters['city'], '=');
    } elseif (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    if (!empty($parameters['address_fuzzy'])) {
        $or = db_or()->condition('A.name', '%' . db_like($parameters['address_fuzzy']) . '%', 'like');
        $query->condition($or);
    }
    $datestr = '';
    if (!empty($parameters['year'])) {//查询条件
        if ($parameters['year']) {
            $datestr .= $parameters['year'] . '-';
        }
    }
    if (!empty($parameters['month'])) {//查询条件
        if ($month = $parameters['month']) {
            if ($month < 10)
                $datestr .= '0' . $parameters['month'];
            else
                $datestr .= $parameters['month'];
        }
    }
    if(!user_access('view all village_activity') && user_access('view own village_activity')){
        $query->condition('V.author_uid',$user->uid, '=');
    }
    if ($datestr) {
        $query->condition('V.date', db_like($datestr) . '%', 'LIKE');
    }
    if (!empty($parameters['title_fuzzy'])) {//查询条件
        $query->condition('V.title', '%' . db_like($parameters['title_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($parameters['content_fuzzy'])) {//查询条件
        $query->condition('V.body', '%' . db_like($parameters['content_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($parameters['attendees_fuzzy'])) {//查询条件
        $query->condition('V.attendee', '%' . db_like($parameters['attendees_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($parameters['distributor'])) {//查询条件
        $query->condition('V.distributor_id', $parameters['distributor'], '=');
    }


    if (isset($form_state['addr_id']) && !empty($form_state['addr_id'])) {
        $query->condition('V.addr_id', $form_state['addr_id']);

    }
    $query->fields('V');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->leftJoin('employee', 'E', 'E.employee_id=V.author_uid');
    //$query->leftJoin('address', 'town', 'addr.town_id=town.id');
    $query->addField('A', 'name', 'village');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('D', 'distributor_name');
    $query->addField('E', 'employee_name');
    //$query->addField('town', 'name', 'town');
    $query->orderBy('V.village_activity_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('C.capacity_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $village_activity_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $village_activity_list[] = $result;
        }
    }
    return  ["data"=>$village_activity_list];
}

function mobile_service_village_activity_add($parameters){
    global $user;
    $module_name = "village_activity";
    $id = !empty($parameters['id'])?$parameters['id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('village_activity') // Table name no longer needs {}
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'distributor_id' => $parameters['distributor_id'],
            'title' => $parameters['title'],
            'body' => $parameters['body'],
            'attendee' => $parameters['attendee'],
            'date' => $parameters['date'],
            'comments' => $parameters['comments'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('village_activity')
        ->fields(array(
            'addr_id' => $parameters['addr_id'],
            'distributor_id' => $parameters['distributor_id'],
            'title' => $parameters['title'],
            'body' => $parameters['body'],
            'attendee' => $parameters['attendee'],
            'date' => $parameters['date'],
            'comments' => $parameters['comments'],
            'timestamp' => time(),))
            -> condition('village_activity_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}
